//
//  HomeViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-6-19.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "XHomeViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "MMDrawerBarButtonItem.h"
#import "NSString+FontAwesome.h"
#import "BBBadgeBarButtonItem.h"
#import "NotificationCenter.h"
#import "MessageService.h"

@interface XHomeViewController ()

@property (nonatomic) BBBadgeBarButtonItem *rightDrawerButton;

@end

@implementation XHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setViewControllers:@[[self.storyboard instantiateViewControllerWithIdentifier:@"matchConViewController"],
                               //[self.storyboard instantiateViewControllerWithIdentifier:@"CEPViewController"],
                               [self.storyboard instantiateViewControllerWithIdentifier:@"foViewController"]]];
    [self setSelectedViewControllerIndex:0];
    
    // Init UI
    [self setupLeftButton];
    [self setupRightButton];
    
}


-(void)setupLeftButton{
    UIButton* leftButton= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [leftButton.titleLabel setFont:[UIFont fontWithName:kFontAwesomeFamilyName size:26]];
    [leftButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-bars"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftDrawerButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [self.navigationItem setLeftBarButtonItem:leftDrawerButton animated:YES];
}

-(void)setupRightButton{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateInfo:)
                                                 name:showMsgInfoNotification
                                               object:nil];
    UIButton* rightButton= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [rightButton.titleLabel setFont:[UIFont fontWithName:kFontAwesomeFamilyName size:22]];
    [rightButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-envelope-o"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    BBBadgeBarButtonItem *rightDrawerButton = [[BBBadgeBarButtonItem alloc] initWithCustomUIButton:rightButton];
    NSString *badgeValue = [NSString stringWithFormat:@"%ld",(long)[[[MessageService alloc] init] countUnreadMsg]];
    rightDrawerButton.badgeValue = badgeValue;
    rightDrawerButton.badgePadding = 1;
    rightDrawerButton.badgeOriginX = 38;
    rightDrawerButton.badgeOriginY = 5;
    self.rightDrawerButton = rightDrawerButton;
    [self.navigationItem setRightBarButtonItem:self.rightDrawerButton animated:YES];
}
-(void)updateInfo:(NSNotification *)notes
{
    NSDictionary* info = [notes userInfo];
    self.rightDrawerButton.badgeValue =[info objectForKey:@"count"];
}

-(void)rightDrawerButtonPress:(id)sender{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
    
}
-(void)leftDrawerButtonPress:(id)sender{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
    
}


@end
